#include <iostream>

#include <deque>

#include <algorithm>
#include <functional>


using std::cout;
using std::endl;

template <typename T>
class Sum
{
public:

	Sum() : s(T()) {}

	T operator()(const T& v)
	{
		s += v;
		return s;
	}

	T value() const { return s; }
private:
	T s;
};

class A
{
public: 
	void print()
	{
		cout << "A::print" << endl;
	}
};

int main()
{
	{
		using namespace std;
		deque<int> v{ 1, 2, 3, 4, 5};
		
		auto s = for_each(v.begin(), v.end(), Sum<int>());
		cout << "Sum: " << s.value() << endl;

		int sum(0);
		function<void(int)> fsum = [&sum](int n)
		{
			sum += n;
		};
		for_each(v.begin(), v.end(), fsum);
		cout << "Sum: " << sum << endl;

	}
	cout << endl;

	{
		using namespace std;

		deque<A> v;
		v.resize(3, A());

		for_each(v.begin(), v.end(), bind(&A::print, placeholders::_1));

		A a;
		auto f = bind(&A::print, placeholders::_1);
		f(a);

	}
	cout << endl;


	return 0;
}






